package com.bbx.taxi.client.DB;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bbx.api.sdk.model.passanger.Return.Coupon.CouponList;
import com.bbx.api.sdk.model.passanger.Return.Coupon.Coupons;
import com.bbx.api.sdk.model.passanger.Return.Coupon.Metadata;
import com.bbx.api.sdk.model.passanger.Return.Coupon.Recommen.Users;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.DB.Util.DBHelper;
import com.bbx.taxi.client.DB.Util.DBcolumns;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 *推荐
 *
 */
public class RecommenDB  implements DBcolumns{
	private DBHelper helper;
	public RecommenDB(Context context) {
		MyApplication myApplication=MyApplication.getInstance();
		helper = DBHelper.getInstance(context,myApplication.getUid());
	}
	/**
	 *  打开只读的数据库
	 */
	public SQLiteDatabase onOpenRead() { 
		SQLiteDatabase SQLdb = helper.getReadableDatabase();
		return SQLdb;
	}

	/**
	 *  打开只写的数据库
	 */
	public SQLiteDatabase onOpenWrite() { 
		SQLiteDatabase SQLdb = helper.getWritableDatabase();
		return SQLdb;
	}
	/**
	 *查询全部
	 */
	public Cursor onQuery() {
		SQLiteDatabase SQLdb=onOpenRead();
		Cursor cursor = SQLdb.query(TABLE_RECOMMEN, null, null, null, null,
				null, RECOMMEN_id + " ");
		return cursor;

	}
	/**
	 *用于分页查询，指定数量，指定id,按降序查询，不能大于id
	 */
	public Cursor onQuery(int id,int count) {
		SQLiteDatabase SQLdb=onOpenRead();
		Cursor cursor = null;
		if(id==-1){
			 cursor = SQLdb.query(TABLE_RECOMMEN, null,null, null, null,
						null, RECOMMEN_id + "  LIMIT 0,"+count);
		}
		else{
			 cursor = SQLdb.query(TABLE_RECOMMEN, null,RECOMMEN_id+"<"+id, null, null,
					null, RECOMMEN_id + "  LIMIT "+count);
		}
		
		return cursor;

	}
	/**
	 * 删除整个表
	 */
	public void onDelete() {
		SQLiteDatabase SQLdb=onOpenWrite();
		SQLdb.delete(TABLE_RECOMMEN, null, null);
		SQLdb.close();
	}

	/**
	 * 删除
	 */
	public void onDelete(String rowId) {
		SQLiteDatabase SQLdb=onOpenWrite();
		 String[] whereArgs = { String.valueOf(rowId) };
		 int i =SQLdb.delete(TABLE_RECOMMEN, RECOMMEN_id + "=?", whereArgs);
		 SQLdb.close();
	}
	/**
	 * 插入一条数据 
	 */
	public void onWrite(String phone,String state, String  headimg) {
		SQLiteDatabase SQLdb=onOpenWrite();
		ContentValues values = new ContentValues();
		values.put(RECOMMEN_PHONE,phone );
		values.put(RECOMMEN_STATE, state);
		values.put(RECOMMEN_HEADIMG, headimg);
		SQLdb.insert(TABLE_RECOMMEN, RECOMMEN_id, values);
		SQLdb.close();
	}
	/**
	 * 写入数据库数据,
	 */
	public void onWriteDB(List<Users> list) {
		for (int i = 0; i < list.size(); i++) {
			String phone = list.get(i).getPhone();
			String state = list.get(i).getState();
			String headimg = list.get(i).getHeadimg();
			onWrite(phone, state, headimg);
		}
	}
	/**
	 * 刚进来的时候读取的
	 */
	public List<Users> onReadDB(int type,int num) {
		List<Users> list=new ArrayList<Users>();
		Cursor c = onQuery(type, num);
		c.moveToFirst();
		if (c.getCount() > 0) {
			do {

				Map<String, String> map = new HashMap<String, String>();
				for (int i = 0; i < c.getColumnCount(); i++) {
					map.put(c.getColumnName(i), c.getString(i));
				}
				Users users = new Users();
				users.setPhone(map.get(DBcolumns.RECOMMEN_PHONE));
				users.setState(map.get(DBcolumns.RECOMMEN_STATE));
				users.setHeadimg(map.get(DBcolumns.RECOMMEN_HEADIMG));
				//0为注册，则过滤
				if(!map.get(DBcolumns.RECOMMEN_STATE).equals("0")){
					list.add(users);
				}
		
			} while (c.moveToNext());
		}
		c.close();
		onColse();
		return list;
	}
	/**
	 * 关闭数据库
	 */
	public void onColse(){
		SQLiteDatabase SQLdb=onOpenWrite();
		SQLdb.close();
	}
}
